﻿package net.wangit.dev.unit;

import java.util.ArrayList;
import java.util.List;

import net.wangit.LabelUtil;
import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.context.LocalContext;
import net.wangit.dev.BeanEnumerationHelper;
import net.wangit.support.ElementWriter;
import net.wangit.view.element.ElementComponent;


public class LayoutElementWriter implements ElementWriter {

	private static String RESOURCE = "components";

	public LayoutElementWriter() {
	}

	public void write(ActionContext ac, StringBuffer html, ElementComponent ec,
			Row row) {
		setScript(html);
		html
				.append("<table id=\"_elements\" cellpadding=\"0\" cellspacing=\"2\" width=\"100%\" class=\"elements\"><tr><td>");
		html.append("<div id=\"_l_toolbar\" area=\"t\" class=\"lt\"></div>");
		html.append("</td><td rowspan=\"3\" class=\"invisbale\">");
		html.append("<div id=\"_l_invisible\" area=\"i\" class=\"li\"></div>");
		html.append("</td></tr><tr><td>");
		html.append("<div id=\"_l_grid\" area=\"g\" class=\"lg\"></div>");
		html.append("</td></tr><tr><td>");
		html.append("<div id=\"_l_hidden\" area=\"h\" class=\"lh\"></div>");
		html.append("</td></tr></table>");
	}

	private static void setScript(StringBuffer html) {
		List<String> basic = new ArrayList<String>();
		List<String> combo = new ArrayList<String>();
		List<String> advanced = new ArrayList<String>();
		MapList ecs = BeanEnumerationHelper.getEnumeration("element");
		for (int i = 0; i < ecs.size(); i++) {
			String id = ecs.getRow(i).get("value");
			int idInt = Integer.parseInt(id);
			if (idInt < 40)
				basic.add(id);
			else if (idInt < 80)
				combo.add(id);
			else
				advanced.add(id);
		}

		ActionContext ac = LocalContext.getLocalContext().getActionContext();
		html.append("<SCRIPT LANGUAGE=\"JavaScript\">\n<!--\n");
		html.append("var _bc= new Tree('_bc','").append(
				LabelUtil.get(ac, "dev", "bc")).append(
				"','','/domain/dev/t/adde1.png');");
		setComponentScript(html, ac, basic, "_bc", "add");
		html.append("var _cc= new Tree('_cc','").append(
				LabelUtil.get(ac, "dev", "cc")).append(
				"','','/common/images/tool/add.png');");
		setComponentScript(html, ac, combo, "_cc", "add");
		html.append("var _ac= new Tree('_ac','").append(
				LabelUtil.get(ac, "dev", "ac")).append(
				"','','/domain/dev/t/adde80.png');");
		setComponentScript(html, ac, advanced, "_ac", "add");
		html.append("var _pc= new Tree('_pc','").append(
				LabelUtil.get(ac, "dev", "contorl")).append(
				"','','/common/images/tool/add.png');");
		setControlScript(html, ac, "_pc");
		html.append("var _bx= new Tree('_bx','").append(
				LabelUtil.get(ac, "dev", "bc")).append(
				"','','/domain/dev/t/change.png');");
		setComponentScript(html, ac, basic, "_bx", "cc");
		html.append("var _cx= new Tree('_cx','").append(
				LabelUtil.get(ac, "dev", "cc")).append(
				"','','/domain/dev/t/change.png');");
		setComponentScript(html, ac, combo, "_cx", "cc");
		html.append("var _ax= new Tree('_ax','").append(
				LabelUtil.get(ac, "dev", "ac")).append(
				"','','/domain/dev/t/change.png');");
		setComponentScript(html, ac, advanced, "_ax", "cc");
		html.append("\n//-->\n</SCRIPT>\n");
	}

	private static void setComponentScript(StringBuffer html, ActionContext ac,
			List<String> components, String var, String method) {
		for (int i = 0; i < components.size(); i++) {
			String id = components.get(i);
			String name = LabelUtil.get(ac, RESOURCE, (new StringBuilder(
					"element.")).append(id).toString());
			html.append(var).append(".add(new Tree('").append(var).append(id)
					.append("','").append(name).append("',").append(method)
					.append(",'/domain/dev/e/e").append(id)
					.append("00.png'));");
		}

	}

	private static void setControlScript(StringBuffer html, ActionContext ac,
			String var) {
		html.append(var).append(".add(new Tree('_pc1','").append(
				LabelUtil.get(ac, "dev", "toolbar.back")).append(
				"',addControl,'/domain/dev/e/e500.png'));");
		html.append(var).append(".add(new Tree('_pc2','").append(
				LabelUtil.get(ac, "dev", "toolbar.save")).append(
				"',addControl,'/domain/dev/e/e511.png'));");
		html.append(var).append(".add(new Tree('_pc3','").append(
				LabelUtil.get(ac, "dev", "toolbar.new")).append(
				"',addControl,'/domain/dev/e/e500.png'));");
		html.append(var).append(".add(new Tree('_pc4','").append(
				LabelUtil.get(ac, "dev", "toolbar.delete")).append(
				"',addControl,'/domain/dev/e/e511.png'));");
		html.append(var).append(".add(new Tree('_pc5','").append(
				LabelUtil.get(ac, "dev", "toolbar.add")).append(
				"',addControl,'/domain/dev/e/e500.png'));");
		html.append(var).append(".add(new Tree('_pc6','").append(
				LabelUtil.get(ac, "dev", "toolbar.remove")).append(
				"',addControl,'/domain/dev/e/e500.png'));");
		html.append(var).append(".add(new Tree('_pc7','").append(
				LabelUtil.get(ac, "dev", "toolbar.clear")).append(
				"',addControl,'/domain/dev/e/e510.png'));");
		html.append(var).append(".add(new Tree('_pc8','").append(
				LabelUtil.get(ac, "dev", "toolbar.query")).append(
				"',addControl,'/domain/dev/e/e510.png'));");
		html.append(var).append(".add(new Tree('_pc9','").append(
				LabelUtil.get(ac, "dev", "link.scan")).append(
				"',addControl,'/domain/dev/e/e1100.png'));");
		html.append(var).append(".add(new Tree('_pc10','").append(
				LabelUtil.get(ac, "dev", "link.edit")).append(
				"',addControl,'/domain/dev/e/e1100.png'));");
		html.append(var).append(".add(new Tree('_pc31','").append(
				LabelUtil.get(ac, "dev", "toolbar.ajax")).append(
				"',addControl,'/domain/dev/e/cajax.png'));");
		html.append(var).append(".add(new Tree('_pc51','").append(
				LabelUtil.get(ac, "dev", "toolbar.excel")).append(
				"',addControl,'/domain/dev/e/cexcel.png'));");
		html.append(var).append(".add(new Tree('_pc52','").append(
				LabelUtil.get(ac, "dev", "toolbar.pdf")).append(
				"',addControl,'/domain/dev/e/cpdf.png'));");
	}

}
